package cm.studapp;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cm.studapp.database.DbManager;

public class WeekView extends Activity {
	 
		GridView gridView;
	 
	    int[] MOBILE_OS;
	    DbManager db;
	    LinearLayout parent;
	 
		@Override
		public void onCreate(Bundle savedInstanceState) {
	 
			super.onCreate(savedInstanceState);
			setContentView(R.layout.week_view_2);
			/*
			gridView = (GridView) findViewById(R.id.gridView1);
			MOBILE_OS = create();
			gridView.setAdapter(new WeekElemAdapter(this, MOBILE_OS));
	 		*/
			parent = (LinearLayout)findViewById(R.id.Weekview2LL_parentView);
			initRows();
			db = new DbManager(this);
			
			setStats();
			
			
	 
		}

		private void setStats() {
			// TODO Auto-generated method stub
			Cursor statInfo = db.getStatBloqsData();
			while(statInfo.moveToNext()){
				int bloque = statInfo.getInt(0);
				int fila = bloque/8;
				int col = bloque%8;
				fila++;
				LinearLayout filaLL =(LinearLayout) parent.getChildAt(fila);
				TextView colTV = (TextView) filaLL.getChildAt(col);
				double dividendo = statInfo.getDouble(1);
				int divisor = statInfo.getInt(2)+statInfo.getInt(1);
				double div = (dividendo/divisor)*100.0;
				int porcentaje = (int) Math.round(div);
				
				if (porcentaje < 40){
					colTV.setBackgroundResource(R.drawable.redshape);
				}
				else if (porcentaje >= 40 && porcentaje <=  60){
					colTV.setBackgroundResource(R.drawable.yellowshape);
				}
				else{
					colTV.setBackgroundResource(R.drawable.greenshape);
				}
				colTV.setText(porcentaje+"%");
			}
			statInfo.close();
			
		}

		private void initRows() {
			// TODO Auto-generated method stub
			
			String hora = "00";
			for(int i =0;i<48;i++){
				LinearLayout child = (LinearLayout)getLayoutInflater().inflate(R.layout.week_row, null);
				TextView horaView = (TextView)child.getChildAt(0);
				String laHora;
				String min;
				
				if(i%2==0){
					min="00";
					hora=""+i/2;
				}
				else{
					min="30";
				}
				if(i<20){
					laHora = "0"+hora+":"+min;
				}
				else
					laHora = hora+":"+min;
				horaView.setText(laHora);
				parent.addView(child);
			}
		}

		private int[] create() {
			// TODO Auto-generated method stub
			int[] aux = new int[8*24];
				for(int i=0;i<70;i++){
					if(Math.random()>0.5){
						aux[i]=1;
					}
					else
						aux[i]=0;
				}
			
			return aux;
		}
	 
}
